package com.kingint.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.dto.BackQueryParam;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.entity.UmsMemberRoleRelation;
import com.kingint.mbg.sys.mapper.UmsMemberMapper;
import com.kingint.mbg.sys.mapper.UmsMemberRoleRelationMapper;
import com.kingint.web.sys.service.MemberBackService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;


@Service
@Slf4j
public class MemberBackServiceImpl implements MemberBackService {
    @Resource
    private UmsMemberMapper memberMapper;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private UmsMemberRoleRelationMapper memberRoleRelationMapper;

    @Override
    public Integer insertMemberInfo(UmsMember member) {
        member.setCreateTime(new Date());
        //密码设置
        String passWord = member.getPassWord();
        String encode = MD5Util.encode(passWord);
        member.setPassWord(encode);//MD5加密
        UmsMember currentMember = adminService.getCurrentMember();
        member.setHotelId(currentMember.getHotelId());
        int insert = memberMapper.insert(member);
        return insert;
    }

    @Override
    public Integer deleteMemberInfo(Long id) {
        UmsMember member = new UmsMember();
        member.setId(id);
        member.setAccountStatus(1);
        int i = memberMapper.updateById(member);
        return i;
    }

    @Override
    public IPage<UmsMember> getAllMemberList(BackQueryParam param, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("当前账号没有关联对应的酒店id");
            return null;
        }
        IPage<UmsMember> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UmsMember> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("account_status", 0);

        String name = param.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("username",name);
        }
        IPage<UmsMember> page1 = memberMapper.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public UmsMember getMemberInfo(Long id) {
        UmsMember member = memberMapper.selectById(id);
        member.setPassWord(null);
        //查询对应的角色信息
        QueryWrapper<UmsMemberRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", id);
        UmsMemberRoleRelation relation = memberRoleRelationMapper.selectOne(wrapper);
        if (!StringUtils.isEmpty(relation)) {
            member.setRoleId(relation.getRoleId());
        }
        return member;
    }

    @Override
    public Integer updateMemberInfo(UmsMember member) {
        String passWord = member.getPassWord();
        if (!StringUtils.isEmpty(passWord)) {
            String encode = MD5Util.encode(passWord);
            member.setPassWord(encode);
        }
        Long roleId = member.getRoleId();//对应角色
        if (StringUtils.isEmpty(roleId)) {
            //删除对应角色信息
            QueryWrapper<UmsMemberRoleRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("member_id", member.getId());
            memberRoleRelationMapper.delete(wrapper);
        } else {
            //修改对应的角色信息
            QueryWrapper<UmsMemberRoleRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("member_id", member.getId());
            Integer integer = memberRoleRelationMapper.selectCount(wrapper);
            if (integer != 0) {
                UmsMemberRoleRelation relation = new UmsMemberRoleRelation();
                relation.setRoleId(roleId);
                memberRoleRelationMapper.update(relation, wrapper);
            } else {
                UmsMemberRoleRelation relation = new UmsMemberRoleRelation();
                relation.setRoleId(roleId);
                relation.setMemberId(member.getId());
                memberRoleRelationMapper.insert(relation);
            }
        }
        //更新用户信息
        int i = memberMapper.updateById(member);
        return i;
    }
}
